#include "main.h"


void test(string addr="8.134.175.37",uint16_t port=23625,sa_family_t family=AF_INET)
{

    int clientfd;
    struct sockaddr_in serveraddr;
    int len, ret;
    char write_buff[128] = {0};

    char buf[][128] = { "P地址配置错误：客户端或服务器的IP地址配置不正确，导致无法通信",
                        "内核模块加载失败：相关网络内核模块没有正确加载，导致TCP连接失败。",
                        "在Ubuntu系统中搭建TCP连接时"                    
    };

    strcpy(write_buff,"hello sercve\r\n");

    cout << "连接服务器" << endl;

    // 　连接服务器

    //计算绑定服务器的结构体大小
    len = sizeof(struct sockaddr);
    //建立套接字
    clientfd = socket(AF_INET, SOCK_STREAM, 0);



    serveraddr.sin_family = family;
    serveraddr.sin_port =  htons(port); 
    serveraddr.sin_addr.s_addr = inet_addr(addr.c_str());

    //连接服务器
    ret = connect(clientfd, (struct sockaddr *)&serveraddr, len);
    if(-1 == ret)
    {
        printf("connect fail\n");
        close(clientfd);
        exit(0);        
    }

    //　发送信息合等待信息
    int count = 0;
    while(1)
    {

        strcpy(write_buff,buf[random()%3]);

        write(clientfd,write_buff,strlen(write_buff));

        read(clientfd,write_buff,sizeof(write_buff));

        printf("pthread_self:%ld  %s\r\n",pthread_self(),write_buff);

        

        usleep(500);

        // if(count  > 200)
        //     break;
        // count++;

        bzero(write_buff, sizeof(write_buff));

    }

    close(clientfd);
    cout << "关闭ｔｃｐ服务" << endl;
}



void ThreadWord(void*arg)
{


    test();

    
}









int main(int argc, char *argv[])
{

    Pool* pool = Pool::GetInstance(10000,1000000000,10000,1);
    TaskNode node;

    node.do_task = ThreadWord;
    node.arg = nullptr; 
    // 添加任务
    for(int pos=0 ;pos < 100000; pos++)
    {
        pool->Add_Task(node);
    }

    for(;;)
    {

        pool->Print_info();
        sleep(1);

    }

    return 0;
}


